var validateArray = new Array();
var errorArray = new Array();
var form_success = true;
/*添加日期类型验证*/
function addDateValidate(webId, rules) {
  var dateInput = $('#'+webId);
  dateInput.addClass('Wdate');
  dateInput.attr('readonly', true);
  for(var i=0; i<rules.length; i++) {
    dateInput.attr('title', dateInput.attr('title')+'['+rules[i].message+']');
  }
  dateInput.bind('click', function(){
    if(rules.length==1)
      eval('WdatePicker({'+rules[0].type+':'+rules[0].value+'})');
    else if(rules.length==2)
      eval('WdatePicker({'+rules[0].type+':\''+rules[0].value+'\','+rules[1].type+':\''+rules[1].value+'\'})');
    else
      eval('WdatePicker()');
  });
}
/*添加方法验证*/
function addFunctionValidate(fuc, message) {
  validateArray.push(function(){
    if(!fuc()) {
      errorArray.push(message);
      form_success = false;
    }
  });
}
/*添加验证*/
function addValidate(webId, rules) {
  for(var i=0; i<rules.length; i++)
  {
    if(rules[i].type=='canNull')
    {
      validateArray.push(function(index){
        return function(){
          validateNullable(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='maxlength')
    {
      validateArray.push(function(index){
        return function(){
          validateMaxlength(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='minlength')
    {
      validateArray.push(function(index){
        return function(){
          validateMinlength(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='maxvalue')
    {
      validateArray.push(function(index){
        return function(){
          validateMaxvalue(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='minvalue')
    {
      validateArray.push(function(index){
        return function(){
          validateMinvalue(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='startwith')
    {
      validateArray.push(function(index){
        return function(){
          validateStartwith(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='endwith')
    {
      validateArray.push(function(index){
        return function(){
          validateEndwith(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
    else if(rules[i].type=='include')
    {
      validateArray.push(function(index){
        return function(){
          validateInclude(webId, rules[index].value, rules[index].message);
        };
      }(i));
    }
  }
}
/*表单验证*/
function validate() {
  form_success = true;
  errorArray = new Array();
  $('#form_error_content').remove();
  for(var i=0; i<validateArray.length; i++)
  {
    validateArray[i]();
  }
  if(form_success)
    return true;
  else
  {
    var errorContent = $('<DIV id="form_error_content" class="validateError"></DIV>').appendTo($(document.body));
    var errorCloser = $('<DIV title="关闭" class="closer"></DIV>').click(function(){
      errorContent.remove();
    }).appendTo(errorContent);
    
    var errorUl = $('<UL></UL>');
    errorUl.appendTo(errorContent);
    //显示错误信息
    for(var i=0; i<errorArray.length; i++)
    {
      if(errorArray[i]!='')
      {
        var errorLi = $('<LI>');
        errorLi.html(errorArray[i]);
        errorUl.append(errorLi);
      }
    }
    return false;
  }
}
/*验证空*/
function validateNullable(objId, nullable, error) {
  var obj = $('#'+objId);
  if(nullable=='F' && obj.val()==='') {
    errorArray.push(error);
    form_success = false;
  }
}
/*验证最长*/
function validateMaxlength(objId, maxlength, error) {
  var obj = $('#'+objId);
  if(obj.val().length > maxlength) {
    errorArray.push(error);
    form_success = false;
  }
}
/*验证最短*/
function validateMinlength(objId, minlength, error) {
  var obj = $('#'+objId);
  if(obj.val().length < minlength) {
    errorArray.push(error);
    form_success = false;
  }
}
/*验证最大*/
function validateMaxvalue(objId, maxvalue, error) {
  var obj = $('#'+objId);
  var objvalue = parseFloat(obj.val());
  maxvalue = parseFloat(maxvalue);
  if(objvalue>maxvalue) {
    errorArray.push(error);
    form_success = false;
  }
}
/*验证最小*/
function validateMinvalue(objId, minvalue, error) {
  var obj = $('#'+objId);
  var objvalue = parseFloat(obj.val());
  minvalue = parseFloat(minvalue);
  if(objvalue<minvalue) {
    errorArray.push(error);
    form_success = false;
  }
}
/*验证以?开始*/
function validateStartwith(objId, str, error) {
  var obj = $('#'+objId);
  if(obj.val().indexOf(str)!=0){
    errorArray.push(error);
    form_success = false;
  }
}
/*验证以?结束*/
function validateEndwith(objId, str, error) {
  var obj = $('#'+objId);
  if(obj.val().indexOf(str)!=(obj.val().length-str.length)){
    errorArray.push(error);
    form_success = false;
  }
}
/*必须包含*/
function validateInclude(objId, str, error) {
  var obj = $('#'+objId);
  if(obj.val().indexOf(str)<0){
    errorArray.push(error);
    form_success = false;
  }
}